// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SerializationContext.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.LiveObjects
{
    #region Usings

    using System.Collections.Generic;
    using System.IO;
    using System.Reflection;

    using Org.OpenTrader.Framework.LiveObjects.Interfaces;

    #endregion

    /// <summary>
    /// The flat file export import context.
    /// </summary>
    public class SerializationContext : ISerializationContext
    {
        #region Constants and Fields

        /// <summary>
        /// The identity.
        /// </summary>
        private static readonly Identity Identity = Identity.Create(MethodBase.GetCurrentMethod());

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SerializationContext"/> class.
        /// </summary>
        /// <param name="workingDirectory">
        /// The working directory.
        /// </param>
        public SerializationContext(string workingDirectory)
        {
            this.BinaryContents = new Dictionary<string, byte[]>();
            this.WorkingDirectory = workingDirectory;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SerializationContext"/> class.
        /// </summary>
        public SerializationContext()
        {
            this.BinaryContents = new Dictionary<string, byte[]>();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets BinaryContents.
        /// </summary>
        public IDictionary<string, byte[]> BinaryContents { get; private set; }

        /// <summary>
        /// Gets WorkingDirectory.
        /// </summary>
        public string WorkingDirectory { get; private set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// The save.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="content">
        /// The content.
        /// </param>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <returns>
        /// </returns>
        public static Status SaveToDisk(string name, byte[] content, ISerializationContext context)
        {
            if (context.WorkingDirectory == null)
            {
                return Status.Failure(Identity, "Save: This Context is not a file based context, cannot save to disk");
            }

            // Write the content to file with given name (the root file)
            File.WriteAllBytes(string.Format(@"{0}\{1}", context.WorkingDirectory, name), content);

            // Write the depends stacked in context
            foreach (var file in context.BinaryContents.Keys)
            {
                var path = string.Format(@"{0}\{1}", context.WorkingDirectory, file);
                File.WriteAllBytes(path, context.BinaryContents[file]);
            }

            return Status.Success(Identity);
        }

        #endregion
    }
}